package com.net.shop.car.manager.ui.viewholder;

import java.util.List;

import android.graphics.Bitmap;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.net.shop.car.manager.BaseActivity;
import com.net.shop.car.manager.R;
import com.net.shop.car.manager.api.model.Goods;
import com.net.shop.car.manager.api.model.Seller;
import com.net.shop.car.manager.api.model.SellerInfo;
import com.net.shop.car.manager.api.volley.VolleyCenter;
import com.net.shop.car.manager.utils.StringUtils;

public class TuoCheViewHolder implements ViewHolder, OnGoodsAddedShopCarListener {

	private ImageView logo;
	private TextView shopName;
	private TextView good;
	private TextView notGood;
	private TextView des;
	private TextView distance;
	private TextView address;

	/**
	 * 
	 */
	private ListView services;

	private ListView tels;
	private BaseActivity activity;
	public JieSuanViewHolder jieSuanViewHolder;

	public TuoCheViewHolder(BaseActivity activity,View v) {
		this.activity = activity;
		logo = (ImageView) v.findViewById(R.id.seller_shop_logo);
		shopName = (TextView) v.findViewById(R.id.seller_shop_name);
		good = (TextView) v.findViewById(R.id.seller_shop_good);

		notGood = (TextView) v.findViewById(R.id.seller_shop_notgood);

		des = (TextView) v.findViewById(R.id.seller_store_des);
		distance = (TextView) v.findViewById(R.id.seller_shop_distance);
		address = (TextView) v.findViewById(R.id.seller_shop_address);
		services = (ListView) v.findViewById(R.id.tuoche_services);
		tels = (ListView) v.findViewById(R.id.tuoche_tel);

		jieSuanViewHolder = new JieSuanViewHolder(
				v.findViewById(R.id.tuoche_pay_panel), activity);
	}

	@Override
	public void setData(Seller seller, final SellerInfo sellerInfo) {
		Bitmap bitmap = sellerInfo.getShopLogoBitmap();
		if (bitmap == null) {
			bitmap = seller.getShopLogoBitmap();
			sellerInfo.setShopLogoBitmap(bitmap);
			if (bitmap == null) {
				VolleyCenter.getVolley().addImageRequest(
						sellerInfo.getShopLogo(),
						new Response.Listener<Bitmap>() {

							@Override
							public void onResponse(Bitmap response) {
								sellerInfo.setShopLogoBitmap(response);
								logo.setImageBitmap(response);
							}
						}, new Response.ErrorListener() {

							@Override
							public void onErrorResponse(VolleyError error) {
//								logo.setImageBitmap(null);// 设置默认图片
							}
						});
			} else {
				logo.setImageBitmap(bitmap);
			}
		} else {
			logo.setImageBitmap(bitmap);
		}
		shopName.setText(sellerInfo.getShopName());
		good.setText(sellerInfo.getGoodNum() + "");
		notGood.setText(sellerInfo.getErrNum() + "");
		des.setText(sellerInfo.getIntroduce());
		distance.setText(StringUtils.parseDistance(seller.getDistance()));
		address.setText(sellerInfo.getShopAddress());

		setTel(sellerInfo.getTels());
		services.setAdapter(new ServiceGoodsAdapter(sellerInfo.getServiceOne(),sellerInfo.isOnlinePay(),
				activity, this));
	}

	private void setTel(final List<String> tel) {
		tels.setAdapter(new TelsAdpater(tel, activity));
	}

	@Override
	public void onGoodsAdded(Goods goods) {
		jieSuanViewHolder.addGoods(goods);
	}

	@Override
	public void onGoodsRemoved(Goods goods) {
		jieSuanViewHolder.addGoods(goods);
	}
}
